package com.dff.jrfw;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.dff.jrfw.databinding.ActivityFingerSetBinding;
import com.dff.jrfw.remote.CustomCallBack;
import com.dff.jrfw.remote.RetrofitUtils;
import com.dff.jrfw.requestBody.BindDeviceDto;
import com.dff.jrfw.utils.DeviceIdUtil;
import com.dff.jrfw.utils.SharedPreferenceUtils;
import com.dff.jrfw.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class FingerSetActivity extends BaseActivity implements View.OnClickListener {

    private ActivityFingerSetBinding binding;
    private String username;
    private boolean isLogin = false;

    public static void callMe(Context context, String username, boolean isLogin) {
        Intent intent = new Intent(context, FingerSetActivity.class);
        intent.putExtra("username", username);
        intent.putExtra("isLogin", isLogin);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_finger_set);
        username = getIntent().getStringExtra("username");
        isLogin = getIntent().getBooleanExtra("isLogin", false);
        initView();
    }

    private void initView() {
        binding.tvTipOne.setText("可通过验证系统指纹\n快速完成登录");
        binding.btnOpenFinger.setOnClickListener(this);
        binding.btnJump.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_open_finger:
                bindDevice();
                break;
            case R.id.btn_jump:
                if (isLogin) {
                    SharedPreferenceUtils.getInstance().saveAccount(FingerSetActivity.this, username);
                    MainActivity.callMe(FingerSetActivity.this);
                }
                finish();
                break;
        }
    }


    private void bindDevice() {
        showLoading();
        BindDeviceDto bindDeviceDto = new BindDeviceDto();
        String deviceId = DeviceIdUtil.getDeviceId(FingerSetActivity.this);
        bindDeviceDto.setDeviceId(deviceId);
        bindDeviceDto.setUno(username);
        bindDeviceDto.setFpSwitch(1);
        RetrofitUtils.getService().bindDevice("1", "1", bindDeviceDto).enqueue(new CustomCallBack<ResponseBody>() {

            @Override
            public void onFailure(Call call, Throwable t) {
                super.onFailure(call, t);
                hideLoading();
            }

            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                hideLoading();
                try {
                    if (response.body() != null) {
                        String result = response.body().string();
                        JSONObject jsonObject = new JSONObject(result);
                        int code = jsonObject.getInt("code");
                        if (code == 200) {
                            if (isLogin) {
                                SharedPreferenceUtils.getInstance().saveAccount(FingerSetActivity.this, username);
                                MainActivity.callMe(FingerSetActivity.this);
                            }
                            finish();
                        } else {
                            String msg = jsonObject.getString("msg");
                            ToastUtil.toast(msg);
                        }
                    } else {
                        ResponseBody errorResponseBody = response.errorBody();
                        if (errorResponseBody != null) {
                            ToastUtil.toast(errorResponseBody.string());
                        }
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


}